package com.situ.javase.day5;

import java.util.Scanner;

public class StudentManger {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入学生数量：");
		int count = scanner.nextInt();
		Student[] array = new Student[count];
		for (int i = 0; i < array.length; i++) {
			System.out.println("请输入学生的id：");
			int id = scanner.nextInt();
			System.out.println("请输入学生的名字：");
			String name = scanner.next();
			System.out.println("请输入学生的年龄：");
			int age = scanner.nextInt();
			System.out.println("请输入学生的班级：");
			String className = scanner.next();
			
			// 每次循环构造一个学生对象
			Student student = new Student(id, name, age, className);
			// 将每次遍历生成的student对象放到数组对应下标的位置
		    array[i]  = student;
		}
		
		printStudentsInfo(array);
		
		// for(;;)
		while (true) {
			System.out.println("----------------------");
			System.out.println("根据id查找输入：1");
			System.out.println("根据名字查找输入：2");
			System.out.println("根据年龄查找输入：3");
			System.out.println("根据班级查找输入：4");
			System.out.println("退出程序：0");
			System.out.println("----------------------");
			int type = scanner.nextInt();
			if (type == 0) {
				System.out.println("退出程序");
				break;
			}
			
			switch (type) {
				case 1:// 根据id查找
					System.out.println("根据id搜索");
					int searchId = scanner.nextInt();
					boolean isSearchByIdFound = false;
					for (int i = 0; i < array.length; i++) {
						Student student = array[i];
						if (student.getId() == searchId) {
							isSearchByIdFound = true;
							System.out.println(student);
							// 如果要求是找到一个就可以
							// break;
						}
					}
					if (isSearchByIdFound = false) {
						System.out.println("没有该Id的学生");
					}
					break;
				case 2:// 根据名字查找
					System.out.println("根据名字搜索");
					String searchName = scanner.next();
					// 查找数组里面学生的名字是不是有要和搜索的名字一样，
					// 1、如果有就打印当前学生的信息，
					// 2、如果搜索完了所有学生都没有找到，打印“没有该姓名的学生”
					// 初始认为：还没找，根据名字没有找到
					boolean isSearchByNameFound = false;
					for (int i = 0; i < array.length; i++) {
						Student student = array[i];
						if (student.getName().equals(searchName)) {
							isSearchByNameFound = true;
							System.out.println(student);
							// 如果要求是找到一个就可以
							// break;
						}
					}
					if (isSearchByNameFound = false) {
						System.out.println("没有该姓名的学生");
					}
					break;
				default:
					break;
			}
		}
		
	}

	private static void printStudentsInfo(Student[] array) {
		for (int i = 0; i < array.length; i++) {
			// System.out.println(array[i]);
			Student student = array[i];
			System.out.println(student);
		}
	}
}
